import re
import unicodedata

from BeautifulSoup import BeautifulSoup, SoupStrainer
import demonyms

#===================================================================

noise_words_pattern = re.compile("(of|the|in|for|at)\\W", re.I)
demonym_pattern = "(?:[ ]*[A-Z][a-z]+[ ])+"
demonym_regex = re.compile(demonym_pattern)

#===================================================================
# I really don't like to do this, but the way Python's re deals with 
# groups made me have separate expressions and "or" them later

place_name_pattern = "(?:[ ]*[A-Z][a-z',]+[ ]*)+"

patterns = ["The"    + place_name_pattern,
            "of the" + place_name_pattern,
            "at"     + place_name_pattern,
            "of"     + place_name_pattern,
            "in the" + place_name_pattern,
            "in"     + place_name_pattern,
            "for"    + place_name_pattern ]

place_name_regex = re.compile("|".join(patterns))

#===================================================================

def text_of(html):
    soup = BeautifulSoup(html)
    return "".join(soup.findAll(text=True))

#===================================================================

def guess_place_names(text):
    places = place_name_regex.findall(text)

    if not places:
        return []

    # remove noise words. Thanks to Manoj Govindan at stackoverflow
    new_places = [noise_words_pattern.sub("", place).strip(", ") for place in places]
    
    return new_places

#===================================================================

def guess_nationalities(birth_event):
    nationalities = list()
    if ',' in birth_event:
        nationality_guess = birth_event.split(",")[1]
        nationalities = [nationality.strip(' ') for nationality in demonym_regex.findall(nationality_guess)]    

    return nationalities

#===================================================================

def lookup_country(nationality):
    try:
        country = demonyms.demonyms[nationality]
        return country
    except KeyError:
        return ''

#===================================================================
